<?php
namespace App\Swoole;

use App\Tasks\ChatTask;
use Hhxsv5\LaravelS\Swoole\Task\Task;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\WebSocket\Frame;
use Swoole\WebSocket\Server;
use Hhxsv5\LaravelS\Swoole\WebSocketHandlerInterface;

class WebSocketService implements WebSocketHandlerInterface
{
    // Declare constructor without parameters
    public function __construct()
    {
    }
    // public function onHandShake(Request $request, Response $response)
    // {
    // Custom handshake: https://www.swoole.co.uk/docs/modules/swoole-websocket-server-on-handshake
    // The onOpen event will be triggered automatically after a successful handshake
    // }
    public function onOpen(Server $server, Request $request)
    {
        $data = [
            'task' => 'open',
            'fd'  => $request->fd,
        ];
        $rst = Task::deliver( new ChatTask( json_encode($data,true) ));
//        $server->push($request->fd, 'Welcome to LaravelS');
    }
    public function onMessage(Server $server, Frame $frame)
    {
        $data = json_decode( $frame->data , true );
        switch($data['type']){
            case 1://登录
                $data = array(
                    'task' => 'login',
                    'params' => array(
                        'name' => $data['name'],
                        'email' => $data['email']
                    ),
                    'fd' => $frame->fd,
                    'roomid' =>$data['roomid']
                );
                if(!$data['params']['name'] || !$data['params']['email'] ){
                    $data['task'] = "nologin";
                }
                $ret = Task::deliver( new ChatTask(json_encode($data)) );
//                echo $frame->fd."登陆\n";
                break;
            case 2: //新消息
                $data = array(
                    'task' => 'new',
                    'params' => array(
                        'name' => $data['name'],
                        'avatar' => $data['avatar']
                    ),
                    'c' => $data['c'],
                    'message' => $data['message'],
                    'fd' => $frame->fd,
                    'roomid' => $data['roomid']
                );
                $ret = Task::deliver( new ChatTask(json_encode($data)) );
//                echo $frame->fd."发了消息\n";
                break;

            case 3: // 改变房间
                $data = array(
                    'task' => 'change',
                    'params' => array(
                        'name'   => $data['name'],
                        'avatar' => $data['avatar'],
                        'email'=>$data['email'],
                    ),
                    'fd' => $frame->fd,
                    'oldroomid' => $data['oldroomid'],
                    'roomid' => $data['roomid']
                );
                $ret = Task::deliver( new ChatTask(json_encode($data)) );
//                echo $frame->fd."改变房间\n";
                break;
            case 4: //私聊信息消息
                $data = array(
                    'task' => 'secretnew',
                    'params' => array(
                        'name' => $data['send_name'],
                        'avatar' => $data['send_avatar']
                    ),
                    'c' => $data['c'],
                    'message' => $data['message'],
                    'send_fd' => $frame->fd,
                    'receive_fd' => $data['receive_fd']
                );
                $ret = Task::deliver( new ChatTask(json_encode($data)) );
//                echo $frame->fd."向".$data['receive_fd']."发起了私聊\n";
                break;
            default :
                $server->push($frame->fd, json_encode(array('code'=>0,'msg'=>'type error')));
        }
    }
    public function onClose(Server $server, $fd, $reactorId)
    {
        //获取登出的用户
        $user = app('swoole')->ws_usersTable->get('user'.$fd);
        if ($user){
            $data = array(
                'task' => 'logout',
                'params' => array(
                    'name' => $user['name'],
                    'roomid'=>$user['roomid']
                ),
                'fd' => $fd
            );
            $ret = Task::deliver( new ChatTask(json_encode($data)) );
        }

        echo "client {$fd} closed\n";
    }
}